<!--
 * @Description: 物料条码规则配置
 * @FilePath: \rs_wms_pc\src\views\basic\productCodeConfig\index.vue
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="SKU(物料编号)" prop="productNo" label-width="110px">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入SKU(物料编号)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="物料名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="条码规则编号" prop="ruleNo">
        <el-input
          v-model="queryParams.ruleNo"
          placeholder="请输入条码规则编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="规则名称" prop="ruleName">
        <el-input
          v-model="queryParams.ruleName"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t("search") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("reset") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:productCodeConfig:add']"
        >{{ $t("add") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:productCodeConfig:edit']"
        >{{ $t("edit") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:productCodeConfig:remove']"
        >{{ $t("delete") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['basic:productCodeConfig:import']"
        >{{ $t("import") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:productCodeConfig:export']"
        >{{ $t("export") }}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productRuleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="编号" align="center" prop="no" /> -->
      <af-table-column label="SKU(物料编号)" align="center" prop="productNo" />
      <af-table-column label="物料名称" align="center" prop="productName" />
      <af-table-column label="物料类型" align="center" prop="productType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_product_type" :value="scope.row.productType" />
        </template>
      </af-table-column>
      <!-- <el-table-column label="条码规则编号" align="center" prop="ruleNo" /> -->
      <af-table-column label="规则名称" align="center" prop="ruleName" />
      <el-table-column :label="$t('createUser')" align="center" prop="createBy" min-width="100"/>
      <el-table-column :label="$t('createTime')" align="center" prop="createTime" width="152" />
      <el-table-column :label="$t('updateUser')" align="center" prop="updateBy" min-width="100"/>
      <el-table-column :label="$t('updateTime')" align="center" prop="updateTime" width="152" />
      <!-- <el-table-column label="规则类型：BARCODE-条码;BATCH-批次号" align="center" prop="ruleType" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:product:edit']"
          >{{ $t("edit") }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:product:remove']"
          >{{ $t("delete") }}</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料条码规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入编号" />
        </el-form-item> -->
        <el-form-item label="物料类型" prop="productType">
          <el-select style="width: 100%" v-model="form.productType" clearable filterable placeholder="请选择物料类型">
            <el-option v-for="dict in dict.type.basic_product_type" :key="dict.value" :label="dict.label" :value="dict.value" @click.native="form.storehouseName = dict.storehouseName;"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="SKU(物料编号)" prop="productNo">
<!--          <el-input v-model="form.productNo" placeholder="请输入物料编号" />-->
          <el-select style="width:100%;" v-model="form.productNo" clearable filterable placeholder="请选择物料编号" @clear="form.productName = ''">
            <el-option
              v-for="dict in productList"
              :key="dict.productNo"
              :label="dict.productNo"
              :value="dict.productNo"
              @click.native="form.productName = dict.productName;"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="物料名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入物料名称" />
        </el-form-item> -->
        <el-form-item label="条码规则" prop="ruleNo">
          <el-select style="width:100%;" v-model="form.ruleNo" filterable placeholder="请输入条码规则">
            <el-option
              v-for="dict in ruleList"
              :key="dict.no"
              :label="dict.ruleName"
              :value="dict.no"
              @click.native="form.ruleName = dict.ruleName;form.ruleType = dict.ruleType;"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="规则名称" prop="ruleName">
          <el-input v-model="form.ruleName" placeholder="请输入规则名称" />
        </el-form-item> -->
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t("sure") }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>


    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          {{ $t("importTip1") }}<em>{{ $t("clickUpload") }}</em>
        </div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--          <div class="el-upload__tip" slot="tip">-->
          <!--            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据-->
          <!--          </div>-->
          <span>{{ $t("importTip") }}</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">{{ $t("download") }}</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">{{ $t("sure") }}</el-button>
        <el-button @click="upload.open = false">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/basic/productCodeConfig";
import { getToken } from "@/utils/auth"
import { listProduct as getListProduct } from "@/api/basic/product";
import { list as getListRule } from "@/api/basic/codeRule";

export default {
  name: "ProductCodeConfig",
  dicts: ["basic_product_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料条码规则表格数据
      productRuleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        productNo: null,
        productName: null,
        productType: null,
        ruleNo: null,
        ruleName: null,
        ruleType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ruleNo: [
          { required: true, message: "条码规则不能为空", trigger: "blur" }
        ],
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的物料数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/product/importExcel",
      },
      // 物料列表
      productList: [],
      ruleList:[]
    };
  },
  created() {
    this.getList();
    this.getListProduct();
    this.getListRule();
  },
  methods: {
    /** 获取物料列表 */
    getListProduct(){
      getListProduct({pageNum:1,pageSize:0}).then(response => {
        this.productList = response.rows;
      });
    },
    /** 获取规则列表 */
    getListRule(){
      getListRule({pageNum:1,pageSize:0,isEnable:'Y'}).then(response => {
        this.ruleList = response.rows;
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "物料条码规则导入"
      this.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download("system/product/importTemplate", {}, `物料条码规则模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    /** 查询物料条码规则列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        productNo: null,
        productName: null,
        productType: null,
        ruleNo: null,
        ruleName: null,
        ruleType: null,
        remark: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料条码规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料条码规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if(!this.form.productType && !this.form.productNo){
          //     this.$modal.msgWarning("物料类型、物料编号不能都为空");
          //   return
          // }
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("editSuc"))
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("addSuc"))
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('deleteTip', { num: ids.length })).then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t("deleteSuc"))
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const params = this.ids && this.ids.length > 0 ? {ids:this.ids} : {...this.queryParams};
      params.pageSize = 0;
      this.download('system/product/export', params, `物料条码规则_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
